<template>
  <div class="config-device">
    <a-row :gutter="24">
      <a-col :md="24" :sm="24">
        <a-card :bordered="false">
          <div class="table-page-search-wrapper">
            <a-form layout="inline">
              <a-row :gutter="0">

                <a-col :md="6" :sm="24">
                  <a-form-item label="所属行业">
                    <a-select style="width: 100%" v-model="queryParam.industryId" allow-clear placeholder="请选择所属行业">
                      <a-select-option v-for="(item,idx) in industryTypeList" :key="idx" :value="item.id">
                        {{ item.industryType }}
                      </a-select-option>
                    </a-select>
                  </a-form-item>
                </a-col>

                <a-col :md="10" :sm="24">
                  <span
                    class="table-page-search-submitButtons"
                    style="float: left; overflow: hidden;padding-left: 16px;">
                    <a-button type="primary" class="btn_search_reset" @click="$refs.table.refresh(true)"><a-icon
                      type="search"/>查询</a-button>
                    <a-button
                      style="margin-left: 8px"
                      class="btn_search_reset"
                      @click="() => {queryParam = {stationId:stationId};$refs.table.refresh(true);}"><a-icon
                        type="reload"/>重置</a-button>
                  </span>
                </a-col>
                <a-col :md="8" :sm="24">
                  <span
                    class="table-page-search-submitButtons"
                    style="float: right; overflow: hidden">
                    <a-button
                      class="btn_add"
                      type="primary"
                      @click="$refs.addForm.add(stationId)"
                      icon="plus">新增</a-button>
                  </span>
                </a-col>
              </a-row>
            </a-form>
          </div>

          <s-table
            id="table-wrapper"
            ref="table"
            size="middle"
            :columns="columns"
            :data="loadData"
            :alert="true"
            :rowKey="(record) => record.id"
          >

            <span slot="fileName" slot-scope="text,record" style="color: #fff">
              <a @click="lookImg(record, '设备正面')">预览</a>
              <a-modal :visible="previewVisible" :title="title" :footer="null" @cancel="previewCancel">
                <img alt="example" style="width: 100%" :src="previewImage" v-if="record.fileName"/>
                <p class="withoutData" v-else>暂无图片</p>
              </a-modal>
            </span>
            <span slot="backFileName" slot-scope="text,record" style="color: #fff">
              <a @click="lookImg(record, '设备反面')">预览</a>
              <a-modal :visible="backPreviewVisible" :title="title" :footer="null" @cancel="previewCancel">
                <img alt="example" style="width: 100%" :src="backPreviewImage"v-if="record.backFileName" />
                <p class="withoutData" v-else>暂无图片</p>
              </a-modal>
            </span>
            <span slot="nameplateFileName" slot-scope="text,record" style="color: #fff">
              <a @click="lookImg(record, '设备铭牌')">预览</a>
              <a-modal :visible="nameplatePreviewVisible" :title="title" :footer="null" @cancel="previewCancel">
                <img alt="example" style="width: 100%" :src="nameplatePreviewImage" v-if="record.nameplateFileName"/>
                <p class="withoutData" v-else>暂无图片</p>
              </a-modal>
            </span>

            <span slot="action" slot-scope="text, record">
              <a @click="$refs.editForm.edit(record)" class="btn_edit">编辑</a>
              <a-divider type="vertical"/>
              <a-popconfirm placement="topRight" title="确认删除？" @confirm="() => del(record)">
                <a class="btn_del">删除</a>
              </a-popconfirm>
            </span>
            <span slot="createdAt" slot-scope="text">
              {{ !!text ? text.substr(0,10): '' }}
            </span>
          </s-table>

          <add-form ref="addForm" @ok="handleOk"/>
          <edit-form ref="editForm" @ok="handleOk"/>
          <channel-marking ref="channelMarking" @ok="handleOk"/>

        </a-card>
      </a-col>
    </a-row>
  </div>
</template>

<script>
  import { STable } from '@/components'
  import { page, del } from '@/api/modular/main/bizdevice/bizDeviceManage'
  import addForm from './device/addForm'
  import editForm from './device/editForm'
  import channelMarking from '../bizdevice/channelMarking'
  import { industryTypeList } from '@/api/modular/main/bizindustryinvolved/bizIndustryInvolvedManage'

  export default {
    components: {
      STable,
      addForm,
      editForm,
      channelMarking
    },

    props: {
      stationId: {
        type: Number,
        require: false
      }
    },

    data () {
      return {
        advanced: false, // 高级搜索 展开/关闭
        queryParam: {}, // 查询参数
        columns: [ // 表头
          {
            title: '设备名称',
            dataIndex: 'deviceName',
            width: 200,
            ellipsis: true
          },
          {
            title: '设备类型',
            dataIndex: 'productModel'
          },
          {
            title: '设备厂商',
            dataIndex: 'productVendor',
            width: 200,
            ellipsis: true
          },
          {
            title: '投产日期',
            dataIndex: 'createdAt',
            width: 120,
            align: 'center',
            scopedSlots: { customRender: 'createdAt' }
          },
          {
            title: '所属行业',
            dataIndex: 'industryType'
          },
          {
            title: '行业分组',
            dataIndex: 'groupId'
          },
          {
            title: '正面',
            dataIndex: 'fileName',
            scopedSlots: { customRender: 'fileName' },
            align: 'center'
          },
          {
            title: '反面',
            dataIndex: 'backFileName',
            scopedSlots: { customRender: 'backFileName' },
            align: 'center'
          },
          {
            title: '铭牌',
            dataIndex: 'nameplateFileName',
            scopedSlots: { customRender: 'nameplateFileName' },
            align: 'center'
          },
          {
            title: '操作',
            dataIndex: 'action',
            scopedSlots: { customRender: 'action' },
            width: 220,
            align: 'center'
          }
        ],
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          return page(Object.assign(parameter, this.queryParam)).then((res) => {
            return res.data
          })
        },
        selectedRowKeys: [],
        selectedRows: [],
        defaultExpandedKeys: [],
        replaceFields: {
          key: 'id'
        },
        treeList: [],
        treeLoading: true,
        index: -1,
        industryTypeList: [],
        url: window.CONST.VUE_APP_API_BASE_URL,
        previewVisible: false,
        backPreviewVisible: false,
        nameplatePreviewVisible: false,
        previewImage: '',
        backPreviewImage: '',
        nameplatePreviewImage: '',
        fileUrl: window.CONST.VUE_APP_API_BASE_FILE_URL,
        title: ''
      }
    },

    created () {
      // console.log('stationId->',this.stationId)
      this.queryParam.stationId = this.stationId
      this.getIndustryTypeList()
    },

    methods: {
      del (record) {
        del(record).then((res) => {
          if (res.success) {
            this.$message.success('删除成功')
            this.$refs.table.refresh()
          } else {
            this.$message.error('删除失败：' + res.message)
          }
        }).catch((err) => {
          this.$message.error('删除错误：' + err.message)
        })
      },
      getIndustryTypeList () {
        industryTypeList().then(res => {
          this.treeLoading = false
          if (!res.success) {
            return
          }
          this.industryTypeList = res.data
        })
      },
      toggleAdvanced () {
        this.advanced = !this.advanced
      },
      handleOk () {
        this.$refs.table.refresh()
      },
      onSelectChange (selectedRowKeys, selectedRows) {
        this.selectedRowKeys = selectedRowKeys
        this.selectedRows = selectedRows
      },
      handleClick (e) {
        const that = this
        if (e.length > 0) {
          that.stationId = e.toString()
          that.queryParam.stationId = e.toString()
          that.handleOk()
        } else {
          that.queryParam.stationId = ''
          that.handleOk()
        }
      },
      look (record) {
        window.open(this.url + '/admin/index1.html#/device_monitor?deviceId=' + record.id)
      },
      lookImg (record, face) {
        this.title = record.deviceName + '\xa0\xa0\xa0\xa0\xa0\xa0' + face
        if (face === '设备正面') {
          this.previewImage = this.fileUrl + record.fileName
          this.previewVisible = true
        } else if (face === '设备反面') {
          this.backPreviewImage = this.fileUrl + record.backFileName
          this.backPreviewVisible = true
        } else if (face === '设备铭牌') {
          this.nameplatePreviewImage = this.fileUrl + record.nameplateFileName
          this.nameplatePreviewVisible = true
        }
      },
      previewCancel () {
        this.previewVisible = false
        this.backPreviewVisible = false
        this.nameplatePreviewVisible = false
      },
      isShow () {
        if (this.stationId) {
          return true
        }
        return false
      }
    }
  }
</script>

<style lang="less" scoped>
  @import '../~@/assets/css/list.less';

  .table-operator {
    margin-bottom: 18px;
  }

  button {
    margin-right: 8px;
  }
  .withoutData{
    text-align: center;
    font-size: 1vw;
    color: #fff;
  }

</style>
